package com.wgl.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wgl.hospclient.inteface.HospitalFeignClient;
import com.wgl.model.enums.OrderStatusEnum;
import com.wgl.model.enums.PaymentStatusEnum;
import com.wgl.model.enums.PaymentTypeEnum;
import com.wgl.model.order.OrderInfo;
import com.wgl.model.order.PaymentInfo;
import com.wgl.model.vo.order.SignInfoVo;
import com.wgl.order.mapper.PaymentMapper;
import com.wgl.order.service.OrderService;
import com.wgl.order.service.PaymentService;
import com.wgl.yygh.common.helper.HttpRequestHelper;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName PaymentServiceImpl.java
 * @Description TODO
 * @createTime 2022年02月14日 22:40:00
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {
    @Resource
    private OrderService orderService;
    @Resource
    private HospitalFeignClient hospitalFeignClient;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //根据id的支付类型查询是否有相同的订单
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_id",orderInfo.getId()).eq("payment_type",paymentType);
        Integer integer = baseMapper.selectCount(queryWrapper);

        if ( integer > 0){
            return;
        }
        //添加
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void paySuccess(String out_trade_no, Map<String, Object> resultMap) {
        //得到支付信息
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper();
        wrapper.eq("out_trade_no",out_trade_no).eq("payment_type", PaymentTypeEnum.ALIPAY.getStatus());
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        //更新支付记录
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setTradeNo(String.valueOf(resultMap.get("tradeNo")));
        paymentInfo.setCallbackContent(resultMap.toString());
        baseMapper.updateById(paymentInfo);

        // 更新订单信息
        OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderService.updateById(orderInfo);
        //调用医院接口 更新订单支付信息
        SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());
        Map<String,Object> reqMap = new HashMap<>();
        reqMap.put("hoscode",orderInfo.getHoscode());
        reqMap.put("hosRecordId",orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);
        JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl() + "/order/updatePayStatus");
    }

    /**
    * @Author: Wgl
    * @Description: 获取支付记录
    * @Date: 2022/2/15 22:47;
    * @param orderId:
     * @param paymentType: ;
    * @return: com.wgl.model.order.PaymentInfo;
    */
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        wrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }
}
